// -----------------------------------------------------------------------------
// quick r3
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// functions
// -----------------------------------------------------------------------------
function changeNumArr(sType, nFlag){
  if(nFlag < 1){changeNumArr.numA = getR3NumArr(); changeNumArr.getNum = true;}
  var l = changeNumArr.numA.length; var i = 0;
  var nArr = changeNumArr.numA;
  var myN = 0; var n1 = 0; var n2 = 0; var n3 = 0;
  var okArr = [];
  switch(sType){
    case 's1':
    case 's2':
      if(sType == 's1'){ myN = 1; }else{ myN = 0; }
      okArr = [];
      for(i = 0; i < l; i++){
        if(( nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1 ) % 2 == myN){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr; 
      break;
    case 's3':
      okArr = [];
      for(i = 0; i < l; i++){
        if(( nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1 ) % 10 > 4 ){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr; 
      break; 
    case 's4':
      okArr = [];
      for(i = 0; i < l; i++){
        if(( nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1 ) % 10 < 5 ){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr; 
      break;
    case 't1' :
      okArr = [];
      for(i = 0; i < l; i++){
        if(( nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1 ) > 13 ){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr; 
      break; 
    case 't2' :
      okArr = [];
      for(i = 0; i < l; i++){
        if(( nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1 ) < 14 ){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr; 
      break; 
    case 'g3' :
    case 'g6' :
      if(sType == 'g3'){ myN = 1; }else{ myN = 0; }
      okArr = [];
      var tempI = 0;
      for(i = 0; i < l; i++){
        tempI = 0;
        if(nArr[i].charAt(0) == nArr[i].charAt(1)){tempI++; }
        if(nArr[i].charAt(0) == nArr[i].charAt(2)){tempI++; }
        if(nArr[i].charAt(1) == nArr[i].charAt(2)){tempI++; }
        if(tempI == myN){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr; 
      break; 

    case 'a1':
    case 'a2':
    case 'a3':
    case 'a4':
      var myNIdx = sType.charAt(1) - 1;
      var tempArr = [3,0,2,1];
      myN = tempArr[myNIdx]; 
      okArr = [];
      var tempI = 0;
      for(i = 0; i < l; i++){
        tempI = 0;
        if(nArr[i].charAt(0) % 2 == 1){ tempI++; }
        if(nArr[i].charAt(1) % 2 == 1){ tempI++; }
        if(nArr[i].charAt(2) % 2 == 1){ tempI++; }
        if(tempI == myN){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr;
      break; 
    case 'b1' : 
    case 'b2' : 
    case 'b3' : 
    case 'b4' : 
      var myNIdx = sType.charAt(1) - 1;
      var tempArr = [3,0,2,1];
      myN = tempArr[myNIdx]; 
      okArr = [];
      var tempI = 0;
      for(i = 0; i < l; i++){
        tempI = 0;
        if(nArr[i].charAt(0) > 4){ tempI++; }
        if(nArr[i].charAt(1) > 4){ tempI++; }
        if(nArr[i].charAt(2) > 4){ tempI++; }
        if(tempI == myN){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr;
      break;
    case 'p1' :
    case 'p2' :
      var tempStr = "2357";
      if(sType == 'p1'){ myN = 1; }else{ myN = 0; }
      var tempI = 0;
      okArr = [];
      for(i = 0; i < l; i++){
        tempI = 0;
        if(tempStr.indexOf(nArr[i].charAt(0)) > -1){ tempI++; }
        if(tempStr.indexOf(nArr[i].charAt(1)) > -1){ tempI++; }
        if(tempStr.indexOf(nArr[i].charAt(2)) > -1){ tempI++; }
        if(myN == 1 && tempI > 0){ okArr.push(nArr[i]); }
        if(myN == 0 && tempI == 0){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr;
      break;
    case 'c2':
    case 'c1':
    case 'c0':
      var myN = sType.charAt(1);
      var tempStr = ",012,123,234,345,456,567,678,789,089,019,";
      var tempStr2 = "19";
      var tempArr = [];
      var tempI = 0;
      okArr = [];
      okArr[0] = [];
      okArr[1] = [];
      okArr[2] = [];
      for(i = 0; i < l; i++){
        tempI = 0;
        tempArr[0] = nArr[i].charAt(1) - nArr[i].charAt(0);
        tempArr[1] = nArr[i].charAt(2) - nArr[i].charAt(0);
        tempArr[2] = nArr[i].charAt(2) - nArr[i].charAt(1);
        if(tempStr2.indexOf(tempArr[0]) > -1){ tempI++; }
        if(tempStr2.indexOf(tempArr[1]) > -1){ tempI++; }
        if(tempStr2.indexOf(tempArr[2]) > -1){ tempI++; }
        if(tempStr.indexOf(nArr[i]) > -1){ okArr[2].push(nArr[i]); }
        else if(tempI > 0){ okArr[1].push(nArr[i]); }
        else{ okArr[0].push(nArr[i]); }
      }
      changeNumArr.numA = okArr[myN];
      break;
       
    case 'm3' : 
    case 'm1' : 
    case 'm0' :
      var myN = sType.charAt(1);
      var tempArr = [];
      var tempI = 0;
      okArr = [];
      for(i = 0; i < l; i++){
        tempI = 0;
        tempArr[0] = nArr[i].charAt(0) % 3;
        tempArr[1] = nArr[i].charAt(1) % 3;
        tempArr[2] = nArr[i].charAt(2) % 3;
        if(tempArr[0] == tempArr[1]){ tempI++; }
        if(tempArr[0] == tempArr[2]){ tempI++; }
        if(tempArr[1] == tempArr[2]){ tempI++; }
        if(myN == tempI){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr;
      break;
       
    case 's2t0' :
    case 's2t2' : 
    case 's2t4' : 
    case 's2t6' : 
    case 's2t8' :
      var myN = sType.charAt(3);
      okArr = [];
      for(i = 0; i < l; i++){
        if(myN == ((nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1) * 2 % 10)){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr;
      break; 
      
    case 'sub2t0' : 
    case 'sub2t2' : 
    case 'sub2t4' : 
    case 'sub2t6' : 
    case 'sub2t8' :
      var myN = sType.charAt(5);
      okArr = [];
      for(i = 0; i < l; i++){
        if(myN == ((nArr[i].charAt(2) * 1 - nArr[i].charAt(0) * 1) * 2 % 10)){ okArr.push(nArr[i]); }
      }
      changeNumArr.numA = okArr;
      break;
       
    default : changeNumArr.numA = []; break;
  }
}

function getR3NumArr(){
  return ["000","001","002","003","004","005","006","007","008","009","011","012","013","014","015","016","017","018","019","022","023","024","025","026","027","028","029","033","034","035","036","037","038","039","044","045","046","047","048","049","055","056","057","058","059","066","067","068","069","077","078","079","088","089","099",  "111","112","113","114","115","116","117","118","119","122","123","124","125","126","127","128","129","133","134","135","136","137","138","139","144","145","146","147","148","149","155","156","157","158","159","166","167","168","169","177","178","179","188","189","199",  "222","223","224","225","226","227","228","229","233","234","235","236","237","238","239","244","245","246","247","248","249","255","256","257","258","259","266","267","268","269","277","278","279","288","289","299",  "333","334","335","336","337","338","339","344","345","346","347","348","349","355","356","357","358","359","366","367","368","369","377","378","379","388","389","399",  "444","445","446","447","448","449","455","456","457","458","459","466","467","468","469","477","478","479","488","489","499",  "555","556","557","558","559","566","567","568","569","577","578","579","588","589","599",  "666","667","668","669","677","678","679","688","689","699",  "777","778","779","788","789","799",  "888","889","899",  "999"];
}

function changeNumArrChk2(){
  var sumTail = $('#sumTail').val();
  var sumVal = $('#sumVal').val();
  var mustNum = $('#mustNum').val();
  if( sumTail == '' && sumVal == '' && mustNum == ''){ return; }
  if( changeNumArr.getNum == false ){
    changeNumArr.numA = getR3NumArr();
  }
  var l = changeNumArr.numA.length;
  if(l < 1){ return ;}
  
  var i = 0;
  var okArr = [];
  var nArr = changeNumArr.numA;
  var tempStr = ''; 
  if(sumTail != ''){
    okArr = [];
    for(i = 0; i < l; i++){
      tempStr = (nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1) % 10;
      if(sumTail.indexOf(tempStr) > -1){ okArr.push(nArr[i]); }
    }
    changeNumArr.numA = okArr;
  }
  if( sumVal != '' ){
    okArr = [];
    sumVal = ',' + sumVal + ',';
    l = changeNumArr.numA.length;
    nArr = changeNumArr.numA;
    for(i = 0; i < l; i++){
      tempStr = ',' + (nArr[i].charAt(0) * 1 + nArr[i].charAt(1) * 1 + nArr[i].charAt(2) * 1) + ',';
      if(sumVal.indexOf(tempStr) > -1){ okArr.push(nArr[i]); }
    }
    changeNumArr.numA = okArr;
  }
  
  if( mustNum != '' ){
    okArr = [];
    l = changeNumArr.numA.length;
    nArr = changeNumArr.numA;
    for(i = 0; i < l; i++){
      if(mustNum.indexOf(nArr[i].charAt(0)) > -1){ okArr.push(nArr[i]); continue; }
      if(mustNum.indexOf(nArr[i].charAt(1)) > -1){ okArr.push(nArr[i]); continue; }
      if(mustNum.indexOf(nArr[i].charAt(2)) > -1){ okArr.push(nArr[i]); continue; } 
    }
    changeNumArr.numA = okArr;
  }
  
}
  
$(function(){
  // change color
  $("#tblQPG input:checkbox").click(function(){
    $(this).parent('label').toggleClass("noClk");
  }); 
  
  // proc quick data
  $("#btnProcData").click(function(){
    $(this).attr("disabled", "disabled");
    changeNumArr.numA = [];
    changeNumArr.getNum = false;
    $("#tblQPG input:checkbox:checked").each(function(idx, ele){
      changeNumArr($(ele).val(), idx);
    });
    changeNumArrChk2();
    var str = '';
    var i = 0; var l = changeNumArr.numA.length;
    $("#totalNumCount").text(0);
    if(l){
      str = '<tr>';
      var nArr = changeNumArr.numA;
      var eachNum = $('#eachNum').val() - 0;
      for(i = 0; i < l ; i++){
        if(i > 0 && i % eachNum == 0){ str += '</tr><tr>'; }
        str += '<td class="onNum">' + nArr[i] + '</td>';
      }
      str += '</tr>';
      $("#tbdShowNumber").html(str);
      $("#tbdShowNumber td").click(function(){
        $(this).toggleClass("onNum");
      });
      $("#totalNumCount").text(l);
    }else{
      $("#tbdShowNumber").html('<tr><td colspan="4" class="c2"> -- </td></tr>');
    }
    $(this).removeAttr("disabled");
    $("#btnSent").focus();
  }); 
  
  // sum Tail Quick
  $('input[name="sumTailChk"]').click(function(){
      var valStr = '';
      switch($(this).val()){
        case '1' : valStr = '13579'; break;
        case '2' : valStr = '02468'; break;
        case '3' : valStr = '56789'; break;
        case '4' : valStr = '01234'; break;
      }
      $('#sumTail').val(valStr);
  }); 
  
  // clear
  $("input:reset").click(function(){
    $("#tblQPG label").removeClass('noClk');   
  }); 
  
  // btnSent    
  $("#btnSent").click(function(){
    var l = $("#tbdShowNumber td.onNum").length;
    if(l < 1){ return false; }
    $(this).attr('disabled');
    var strArr = new Array();
    $("#tbdShowNumber td.onNum").each(function(idx, ele){
      strArr.push($(ele).text());
    });
    if(strArr.length > 0) { $('#iptQuick01Val').val(strArr.join()).focus(); $("#tbdShowNumber").empty(); $("#totalNumCount").text(0);}
  });
});

